<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>{$Think.config.site.title} - 用户管理</title>
    <meta name="keywords" content="{$Think.config.site.keywords}">
    <meta name="description" content="{$Think.config.site.description}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-7">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{$title}</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>

                </div>
                <div class="ibox-content">
                    <form method="get" class="form-horizontal">
                        <input type="hidden" name="id" value="{$user['id'] ?? ''}" />
                        <input type="hidden" name="avatar" value="{$user['avatar'] ?? ''}" />
                        <div class="form-group">
                            <label class="col-sm-2 control-label">{:lang('用户名')}</label>
                            <div class="col-sm-10">
                                <input id="username" name="username" class="form-control" type="text" value="{$user['username'] ?? ''}">

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" style="color:red">{:lang('密码')}</label>
                            <div class="col-sm-10">
                                <input type="password" id="password" name="password" class="form-control" type="text" value="">

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" style="color:red">{:lang('确认密码')}</label>
                            <div class="col-sm-10">
                                <input type="password" id="surepassword" name="surepassword" class="form-control" type="text" value="">

                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">{:lang('姓名')}</label>
                            <div class="col-sm-10">
                                <input id="name" name="name" class="form-control" type="text" value="{$user['name'] ?? ''}">

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">{:lang('昵称')}</label>
                            <div class="col-sm-10">
                                <input id="nickname" name="nickname" class="form-control" type="text" value="{$user['nickname'] ?? ''}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">{:lang('手机号码')}</label>
                            <div class="col-sm-10">
                                <input id="phone" name="phone" class="form-control" type="text" value="{$user['phone'] ?? ''}">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">

                                <input type="submit" class="btn btn-primary" value="{:lang('提交')}">

                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>


        <div class="col-sm-5">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{:lang('更多信息')}</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                {if ( isset($user) )}
                <div class="ibox-content">
                    <p><img id="avatar" width=200 height=200 alt="image" src="{if condition="$user['avatar'] neq ''" }{$Think.config.resourceurl,$user.avatar} {else /} {$Think.config.site.resource_url}images/a1.png {/if}" /></p>
                    <p>{:lang('用户名')}：{$user['username']}</p>
                    <p>{:lang('权限组')}：{$groupname}</p>
                    <p>{:lang('上次登录时间')}：{$user['last_login_time']}</p>
                    <p>{:lang('上次登录IP')}：{$user['last_login_ip']}</p>
                </div>
                {/if}
            </div>
        </div>

    </div>
</div>

<script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
<script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
<script src="{$Think.config.site.resource_url}js/content.min.js?v=1.0.0"></script>
<script src="{$Think.config.site.resource_url}js/plugins/validate/jquery.validate.min.js"></script>
<script src="{$Think.config.site.resource_url}js/plugins/validate/messages_zh.min.js"></script>
<script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
</body>

</html>
<script>
    $(document).ready(function(){
        $("form").validate({
            debug:true,
            submitHandler:function(form){
                save();
            },
            rules: {
                username: {
                    required: true,
                    minlength: 4,
                },
                password: {
                    minlength: 6,
                },
                surepassword : {
                    equalTo: "#password"
                },
                phone: {
                    number: true,
                    minlength: 6,
                }
            },
            messages: {
                username: {
                    required: "{:lang('用户名必须填写')}",
                    minlength: "{:lang('用户名不能少于4位')}",
                },
                password: {
                    minlength: "{:lang('密码不能少于6位')}",
                },
                surepassword : {
                    equalTo: "{:lang('两次密码不同')}"
                },
                phone: {
                    number: "{:lang('电话号码只能包含数字')}",
                    minlength: "{:lang('电话号码不能少于6位')}",
                }
            }
        });

        function save(){
            var fomrData = $("form").serialize();
            var action = 'add';
            if( $("input[name='id']").val() > 0) {
                action = 'edit';
            }
            $.ajax({
                type : 'POST',
                url : '<?php echo \think\Request::instance()->root(); ?>/<?php echo \think\Request::instance()->module(); ?>/User/' + action,
                dataType : 'json',
                data : fomrData,
                success : function(result) {
                    //layer.close(layer1);
                    if(0 === result.code) {
                        if(result.data) {
                            //新增
                            var data = result.data;
                        } else {
                            //修改
                            var data = {
                                username : $("#username").val(),
                                name : $("#name").val(),
                                nickname : $("#nickname").val(),
                                phone : $("#phone").val(),
                                avatar : $("#avatar").attr('src')
                            }
                        }
                        parent.editSuccess(data);

                    }  else  {
                        //修改失败
                        layer.msg(result.msg, {icon: 5});
                    }
                },
                error : function(){
                    layer.msg("{:lang('服务器错误！请稍后重试')}", {icon: 2});
                }
            })
        };
    })
</script>